/**
 * 
 */
package uit.comstore.model.blo;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import uit.comstore.model.entity.Customer;
import uit.comstore.utils.HibernateSessionFactory;

/**
 * @author XIII
 *
 */
public class CustomerBLO {

	@SuppressWarnings("unchecked")
	public List<Customer> list() {
		List<Customer> customers = null;
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from Customer where deleted = :value ");
		query.setParameter("value", 0);
		customers = query.list();
		session.getTransaction().commit();
		return customers;
	}

	public Customer add(Customer customer) {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(customer);
		session.getTransaction().commit();
		return customer;
	}

	public Customer delete(Long id) {
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Customer customer = (Customer) session.load(Customer.class, id);
		customer.setDeleted(1);
		session.getTransaction().commit();
		return customer;
	}

	public Customer update(Customer cus){
		Session session = HibernateSessionFactory.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Customer customer = (Customer)session.get(Customer.class, cus.getId());
		customer.setName(cus.getName());
		customer.setPassword(cus.getPassword());
		customer.setAddress(cus.getAddress());
		customer.setEmail(cus.getEmail());
		customer.setGender(cus.getGender());
		customer.setPhone(cus.getPhone());
		customer.setLastModified(new Date());
		session.update(customer);
		session.getTransaction().commit();
		session.close();
		return customer;
	}
}
